package com.fei.service.impl;

import com.fei.service.DoubleVideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;

@Service
@Slf4j
public class DoubleVideoServiceImpl implements DoubleVideoService {

    @Value("${file.upload.dir}")
    private String realPath;

    @Async("taskExecutor")
    public void doubleRecordTest(String path, String fileName, MultipartFile file) throws Exception{
        long questionId = System.currentTimeMillis();
        FileOutputStream fs = null;
        BufferedInputStream bs = null;
        File filePath ;
        filePath = new File(realPath);
        System.out.println("进入老路径..");
        System.out.println(filePath);
        //没有文件夹就创建文件夹
        if(!filePath.exists()){
            filePath.mkdirs();
        }
        File fl;
        //获取文件名称
        String originalFilename = file.getOriginalFilename();
        //获取文件后缀
        String tx = originalFilename.substring(originalFilename.lastIndexOf("."));
        fl = new File(realPath + "3" + tx);
        log.info("开始创建文件夹");
        InputStream inputStream = file.getInputStream();

        /**
         * inputStream转存
         */
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len;
        while ((len = inputStream.read(buffer)) > -1 ) {
            baos.write(buffer, 0, len);
        }
        baos.flush();

        log.info("开始创建文件夹");
        //创建文件
        fl.createNewFile();
        log.info("创建文件夹成功-----准备进入公共上传步骤!!!");
        boolean countNumber = commonUpload(new ByteArrayInputStream(baos.toByteArray()), new FileOutputStream(fl));
        int number = 1;
        if(countNumber){
            log.info("一次视频上传成功-----------------------------------All");
            //视频通知接口
            log.info("一次通知成功"+"  note:");
            //暂存接口
            log.info("一次暂存成功"+"  noteTran:");
        }else{
            while(number <= 2){
                boolean b = commonUpload(new ByteArrayInputStream(baos.toByteArray()), new FileOutputStream(fl));
                if(b){
                    log.info("第" + number + "次视频上传成功-----------------------------------All");
                    //视频通知接口
                    log.info("第" + number + "次通知成功");
                    //暂存接口
                    log.info("第" + number + "次暂存成功");
                    break;
                }
                number++;
            }
        }
        if(fs!= null){
            try {
                fs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(bs!=null) {
            try {
                bs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public boolean commonUpload(ByteArrayInputStream bs, FileOutputStream fs){
        log.info("进入公共文件上传!!!");
        boolean flag = false;
        try {
            byte [] bytes = new byte[1024];
            int line ;
            //write
            while((line = bs.read(bytes))!= -1){
                fs.write(bytes, 0, line);
                fs.flush();
            }
            log.info("公共视频上传成功-----------------------------------All");
            flag = true;
            return flag;
        } catch (Exception e) {
            e.printStackTrace();
            log.info("文件上传失败"+e.getMessage());
            flag = false;
            return flag;
        }finally {
            if(fs!= null && flag == true){
                try {
                    fs.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bs!=null && flag == true) {
                try {
                    bs.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
